home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.04 Apr 95 / Performance / Fractal 8 / TriPatch.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-13  |  5.3 KB  |  215 lines  |  [TEXT/MPS ]

  1. /*    
  2.     File:        TriPatch.c
  3.  
  4.     Used to build:    “Fractal 7”
  5.     
  6.     Written by:        Dan Clifford        November 1994
  7.  
  8.     Description:
  9.         The following code implements a patch-shading algorithm
  10.         for doing surface shading.
  11. */
  12.  
  13.  
  14.  
  15. #include "Fractal.h"
  16.  
  17. unsigned char *gBlitData = NULL;
  18. Rect gBlitBounds = { 0, 0, 0, 0 };
  19. short gBlitRowBytes = 0;
  20.  
  21.  
  22. /*
  23.     SetBlitBuffer
  24.     
  25.     Call this function to specify the blitting buffer as well as the 
  26.     bounds and the number of bytes per row.
  27. */
  28. void SetBlitBuffer(unsigned char *blitData, Rect *blitBounds, short blitRowBytes)
  29. {
  30.     gBlitData = blitData;
  31.     gBlitRowBytes = blitRowBytes;
  32.     gBlitBounds = *blitBounds;
  33. }
  34.  
  35.  
  36. /*
  37.     FillTriangle
  38.     
  39.     This function fills in a triangular patch of pixels. It assumes the offscreen
  40.     buffer is an 8-byte buffer, and fills in each pixel with the specified color.
  41. */
  42. void FillTriangle(long vertex1H, long vertex1V, long vertex2H, long vertex2V, 
  43.         long vertex3H, long vertex3V, unsigned char color)
  44. {
  45.     if (vertex2V < vertex1V) {
  46.         long    temp;
  47.  
  48.         temp = vertex2V;
  49.         vertex2V = vertex1V;
  50.         vertex1V = temp;
  51.  
  52.         temp = vertex2H;
  53.         vertex2H = vertex1H;
  54.         vertex1H = temp;
  55.     }
  56.     
  57.     if (vertex3V < vertex1V) {
  58.         long    temp;
  59.  
  60.         temp = vertex3V;
  61.         vertex3V = vertex1V;
  62.         vertex1V = temp;
  63.  
  64.         temp = vertex3H;
  65.         vertex3H = vertex1H;
  66.         vertex1H = temp;
  67.     }
  68.     
  69.     if (vertex3H < vertex2H) {
  70.         long    temp;
  71.  
  72.         temp = vertex3V;
  73.         vertex3V = vertex2V;
  74.         vertex2V = temp;
  75.  
  76.         temp = vertex3H;
  77.         vertex3H = vertex2H;
  78.         vertex2H = temp;
  79.     }
  80.  
  81.     if (vertex3V > vertex2V) {
  82.         long            currentLeftH,
  83.                         currentRightH,
  84.                         currentLeftIncrement,
  85.                         currentRightIncrement;
  86.         long            currentV; 
  87.         unsigned char    *currentPixel;
  88.                 
  89.         currentLeftH = (long)(vertex1H) << 16;
  90.         currentRightH = currentLeftH;
  91.  
  92.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  93.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  94.             
  95.         for (currentV = vertex1V; currentV < vertex2V; currentV++) {
  96.  
  97.             long    currentRightHlong = currentRightH >> 16;
  98.             long    currentLeftHlong = currentLeftH >> 16;
  99.             long    currentH;
  100.             
  101.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  102.  
  103.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  104.                 *currentPixel = color;
  105.                 currentPixel++;
  106.             }
  107.             
  108.             currentLeftH += currentLeftIncrement;
  109.             currentRightH += currentRightIncrement;
  110.         
  111.         }
  112.  
  113.         currentLeftH = (long)(vertex2H) << 16;
  114.         currentLeftIncrement = ((long)(vertex3H - vertex2H) << 16) / (vertex3V - vertex2V);
  115.  
  116.         for (currentV = vertex2V; currentV <= vertex3V; currentV++) {
  117.             long    currentRightHlong = currentRightH >> 16;
  118.             long    currentLeftHlong = currentLeftH >> 16;
  119.             long    currentH;
  120.             
  121.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  122.  
  123.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  124.                 *currentPixel = color;
  125.                 currentPixel++;
  126.             }
  127.             
  128.             currentLeftH += currentLeftIncrement;
  129.             currentRightH += currentRightIncrement;
  130.         }
  131.     }
  132.  
  133.     else if (vertex2V > vertex3V) {
  134.         long            currentLeftH,
  135.                         currentRightH,
  136.                         currentLeftIncrement,
  137.                         currentRightIncrement;
  138.         long            currentV; 
  139.         unsigned char    *currentPixel;
  140.                 
  141.         currentLeftH = (long)(vertex1H) << 16;
  142.         currentRightH = currentLeftH;
  143.  
  144.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  145.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  146.             
  147.         for (currentV = vertex1V; currentV < vertex3V; currentV++) {
  148.             long    currentRightHlong = currentRightH >> 16;
  149.             long    currentLeftHlong = currentLeftH >> 16;
  150.             long    currentH;
  151.             
  152.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  153.  
  154.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  155.                 *currentPixel = color;
  156.                 currentPixel++;
  157.             }
  158.             
  159.             currentLeftH += currentLeftIncrement;
  160.             currentRightH += currentRightIncrement;
  161.         }
  162.  
  163.         currentRightH = (long)(vertex3H) << 16;
  164.         currentRightIncrement = ((long)(vertex2H - vertex3H) << 16) / (vertex2V - vertex3V);
  165.         
  166.         for (currentV = vertex3V; currentV <= vertex2V; currentV++) {
  167.             long    currentRightHlong = currentRightH >> 16;
  168.             long    currentLeftHlong = currentLeftH >> 16;
  169.             long    currentH;
  170.             
  171.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  172.  
  173.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  174.                 *currentPixel = color;
  175.                 currentPixel++;
  176.             }
  177.             
  178.             currentLeftH += currentLeftIncrement;
  179.             currentRightH += currentRightIncrement;
  180.         }
  181.     }
  182.     else {
  183.         long            currentLeftH,
  184.                         currentRightH,
  185.                         currentLeftIncrement,
  186.                         currentRightIncrement;
  187.         long            currentV; 
  188.         unsigned char    *currentPixel;
  189.                 
  190.         currentLeftH = (long)(vertex1H) << 16;
  191.         currentRightH = currentLeftH;
  192.  
  193.         currentLeftIncrement = ((long)(vertex2H - vertex1H) << 16) / (vertex2V - vertex1V);
  194.         currentRightIncrement = ((long)(vertex3H - vertex1H) << 16) / (vertex3V - vertex1V);
  195.             
  196.         for (currentV = vertex1V; currentV < vertex3V; currentV++) {
  197.             long    currentRightHlong = currentRightH >> 16;
  198.             long    currentLeftHlong = currentLeftH >> 16;
  199.             long    currentH;
  200.             
  201.             currentPixel = gBlitData + (currentV * gBlitRowBytes) + currentLeftHlong;
  202.  
  203.             for (currentH = currentLeftHlong; currentH <= currentRightHlong; currentH++) {
  204.                 *currentPixel = color;
  205.                 currentPixel++;
  206.             }
  207.             
  208.             currentLeftH += currentLeftIncrement;
  209.             currentRightH += currentRightIncrement;
  210.         }
  211.     }
  212. }
  213.  
  214.  
  215.